package com.bmps.springcloud.insuredmicroservice.service.amnt.strategy;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.trail.TrialPersonModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import com.bmps.springcloud.insuredmicroservice.util.trial.TrialUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * description:
 *
 * @author jackdaw
 * @date 2020-06-15 13:35
 */

public class Lbp030AmntStrategy implements AmntStrategy {

    private static final Logger logger = LoggerFactory.getLogger(Lbp030AmntStrategy.class);

    @Override
    public TrialPersonModel amntTrialCalculation(ProductModel productModel, RequestModel requestModel, RedisService redisService) {
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(productModel.getProdInsSeq(), insurantModelList);

        TrialPersonModel trialPersonModel = new TrialPersonModel();
        String riskCode = productModel.getProdCode();
        trialPersonModel.setRiskCode(riskCode);

        trialPersonModel.setPrem(productModel.getPrem());
        trialPersonModel.setInsuYear(productModel.getInsureYear());
        trialPersonModel.setAppAge(DateUtil.getYears(insurantModel.getBirthday(), requestModel.getPolicyInfo().getPolApplyDate()));
        trialPersonModel.setSex(insurantModel.getSex());

        StringBuilder sb = new StringBuilder(16);
        sb.append(AcceptConstant.REDIS_PREFIX_R004).append("|").append(riskCode);
        Object object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        TrialUtil.setSpecialElement(trialPersonModel, object.toString());

        sb.setLength(0);
        sb.append(AcceptConstant.REDIS_PREFIX_R006).append("|").append(trialPersonModel.getRiskCode()).
                append("|").append(AcceptConstant.CALCULATION_TYPE_P);
        object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        trialPersonModel.setFormula(object.toString());
        logger.info("【" + productModel.getProdCode() + "】【公式】【KEY】" + sb.toString());

        sb.setLength(0);
        sb.append(AcceptConstant.REDIS_PREFIX_R001).append("|").append(riskCode);
        logger.info("【" + riskCode + "】【产品计算要素配置】【KEY】" + sb.toString());
        object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        String result = TrialUtil.getRateKey(trialPersonModel, object.toString(), AcceptConstant.CALCULATION_TYPE_P);
        object = redisService.get(result);
        if (object == null) {
            return new TrialPersonModel();
        }
        TrialUtil.setElementRate(trialPersonModel, object.toString());

        sb.setLength(0);
        sb.append(AcceptConstant.REDIS_PREFIX_R005).append("|").append(riskCode).
                append("|").append(AcceptConstant.CALCULATION_TYPE_P);
        object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        logger.info("【" + riskCode + "】【产品计算公式要素配置】【KEY】" + sb.toString());
        TrialUtil.setElementMapping(trialPersonModel, object.toString());
        return trialPersonModel;

    }
}
